package com.yang.service.adminimpl;

import com.github.pagehelper.PageHelper;
import com.yang.bean.MarketAD;
import com.yang.bean.MarketADExample;
import com.yang.bean.common.BasePageInfo;
import com.yang.bean.common.BaseRespVo;
import com.yang.bean.common.CommonData;
import com.yang.mapper.MarketADMapper;
import com.yang.service.admin.AdminAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminAdServiceImpl implements AdminAdService {
    @Autowired
    MarketADMapper marketADMapper;

    @Override
    public BaseRespVo<CommonData<MarketAD>> queryAllAd(String name, String content, BasePageInfo info) {
        PageHelper.startPage(info.getPage(),info.getLimit());
        MarketADExample example = new MarketADExample();
        example.setOrderByClause(info.getSort() + " "+ info.getOrder());
        MarketADExample.Criteria criteria=example.createCriteria();
        if (name !=null && !" ".equals(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if (content !=null && !" ".equals(content)){
            criteria.andContentLike("%"+content+"%");
        }
        List<MarketAD> marketADS = marketADMapper.selectByExample(example);

        BaseRespVo<CommonData<MarketAD>> baseRespVo = new BaseRespVo<CommonData<MarketAD>>();

        CommonData<MarketAD> marketADCommonData = new CommonData<>();


        marketADCommonData.setList(marketADS);
        baseRespVo.setData(marketADCommonData);
        return baseRespVo;
    }

    @Override
    public BaseRespVo<MarketAD> addAd(MarketAD marketAD) {
        BaseRespVo<MarketAD> baseRespVo = new BaseRespVo<>();
        marketADMapper.insertSelective(marketAD);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        MarketAD marketAD1 = new MarketAD();
        marketAD1.setAddTime(marketAD.getAddTime());
        marketAD1.setContent(marketAD.getContent());
        marketAD1.setDeleted(marketAD.getDeleted());
        marketAD1.setEnabled(marketAD.getEnabled());
        marketAD1.setId(marketAD.getId());
        marketAD1.setLink(marketAD.getLink());
        marketAD1.setName(marketAD.getName());
        marketAD1.setPosition(marketAD.getPosition());
        marketAD1.setStartTime(marketAD.getStartTime());
        marketAD1.setUpdateTime(marketAD.getUpdateTime());
        marketAD1.setUrl(marketAD.getUrl());
        marketAD1.setEndTime(marketAD.getEndTime());
        baseRespVo.setData(marketAD1);
        return baseRespVo;
    }

    @Override
    public BaseRespVo<MarketAD> updateAd(MarketAD marketAD) {
        BaseRespVo<MarketAD> baseRespVo = new BaseRespVo<>();
//        MarketAD record = new MarketAD();
        MarketADExample example = new MarketADExample();
        MarketADExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketAD.getId());
        marketADMapper.updateByExample(marketAD, example);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(marketAD);
        return baseRespVo;
    }

    @Override
    public void deleteAd(MarketAD marketAD) {
        marketADMapper.deleteByPrimaryKey(marketAD.getId());
    }


}
